package com.bawei.module_main.weiget

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_mvi.Router

/**
 *作者：lxy
 *功能:
 *时间：2025/6/10
 */
class CountDownSkipView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    // 画笔
    private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = 5f
        color = Color.WHITE
    }

    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.WHITE
        textAlign = Paint.Align.CENTER
    }

    private val progressPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        strokeWidth = 5f
        color = Color.WHITE
    }


    // 动画相关
    private var countAnimator: ValueAnimator? = null
    private var currentCount = 3
    private var progressSweepAngle = 0f

    // 位置信息
    private val progressRect = RectF()

    // 跳过监听
    var onSkipListener: (() -> Unit)? = null

    init {
        // 设置点击事件
        setOnClickListener {
            onSkipListener?.invoke()
            stopCountDown()
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val centerX = width / 2f
        val radius = minOf(width, height) / 4.5f

        // 2. 绘制圆形边框
        canvas.drawCircle(centerX, height / 3f, radius, circlePaint)

        // 3. 绘制进度圆弧
        progressRect.set(
            centerX - radius,
            height / 3f - radius,
            centerX + radius,
            height / 3f + radius
        )
        canvas.drawArc(progressRect, -90f, progressSweepAngle, false, progressPaint)

        // 4. 绘制倒计时文字
        textPaint.textSize = radius
        canvas.drawText(
            "${currentCount}s",
            centerX,
            height / 3f + radius / 3,
            textPaint
        )

        // 5. 绘制"跳过"文字
        textPaint.textSize = radius * 0.6f
        canvas.drawText("跳过", centerX, height / 3f + radius * 2, textPaint)
    }


    // 开始倒计时
    fun startCountDown() {
        stopCountDown()
        countAnimator = ValueAnimator.ofInt(3, 0).apply {
            duration = 3000
            addUpdateListener { animation ->
                currentCount = animation.animatedValue as Int
                // 计算进度角度 (0-360)
                progressSweepAngle = 360f * (1 - animation.animatedFraction)
                invalidate()

                // 倒计时结束
                if (currentCount == 0) {
                    onSkipListener?.invoke()
                }
            }
            start()
        }
    }

    private fun stopCountDown() {
        countAnimator?.cancel()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stopCountDown()
    }

}